package com.yuanqi.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuanqi.Common.Pojo.Dto.UserDto;
import com.yuanqi.Common.Pojo.Dto.UserInfoDto;
import com.yuanqi.Common.Pojo.Vo.UserLoginVo;
import com.yuanqi.Common.Pojo.Vo.UserPageVo;
import com.yuanqi.Common.Pojo.Vo.UserVo;
import com.yuanqi.Common.Pojo.entity.User;
import com.yuanqi.Common.Pojo.entity.UserBind;
import com.yuanqi.Common.Pojo.entity.Userinfo;
import com.yuanqi.Mapper.UserInfoMapper;
import com.yuanqi.Return.Result;
import com.yuanqi.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/v1/user")
@CrossOrigin
public class UserController {

    @Resource
    UserService userService;
    @Resource
    UserInfoMapper userInfoMapper;

    /**
     * 获取手机验证码
     *
     * @param user 手机号
     * @return
     */
    @PostMapping("/phoneCaptcha")
    public Result phoneCaptcha(@RequestBody User user) {


        String captcha = userService.phoneCaptcha(user.getPhone());
        return Result.success(captcha);
    }

    /**
     * 手机验证码登录/注册
     *
     * @param user 手机号码和验证码
     * @return
     */
    @PostMapping("/phonelogin")
    public Result phonelogin(@RequestBody UserDto user) {
        UserLoginVo userLoginVo = userService.phonelogin(user);
        userLoginVo.getUserInfo().setPassword("");
        return Result.success(userLoginVo);
    }

    /**
     * 账号密码登录
     *
     * @param user 手机号/邮箱  密码
     * @return
     */
    @PostMapping("/UsernameLogin")
    public Result UsernameLogin(@RequestBody UserDto user) {
        UserLoginVo userLoginVo = userService.UsernameLogin(user);
        userLoginVo.getUserInfo().setPassword("");
        return Result.success(userLoginVo);
    }

    /**
     * 第三方登录
     *
     * @param user 第三方账号（openid userpic）
     * @return
     */
    @PostMapping("/otherlogin")
    public Result otherlogin(@RequestBody UserBind user) {
        UserLoginVo userLoginVo = userService.otherlogin(user);
        userLoginVo.getUserInfo().setPassword("");
        return Result.success(userLoginVo);
    }

    /**
     * 第三方绑定
     *
     * @param user 第三方账号（openid userpic）手机号和验证码
     * @return
     */
    @PostMapping("/otherBind")
    public Result otherBind(@RequestBody UserDto user) {
        UserLoginVo userLoginVo = userService.otherBind(user);
        return Result.success(userLoginVo);
    }

    /**
     * 修改密码和忘记密码
     *
     * @param user
     * @return
     */
    @PostMapping("/updatePass")
    public Result updatapass(@RequestBody UserDto user) {
        userService.updatapass(user);
        return Result.success();
    }

    @PostMapping("/updateUserInfo")
    public Result updateUserInfo(@RequestBody UserInfoDto userinfo) {
        userService.updateUserInfo(userinfo);
        return Result.success();
    }


    /**
     * 搜索用户
     *
     * @param userDto
     * @return
     * @throws InterruptedException
     */
    @PostMapping("/searchUser")
    public Result searchUser(@RequestBody UserDto userDto) throws InterruptedException {
        List<UserVo> list = userService.searchUser(userDto);
        return Result.success(list);
    }

    @GetMapping("/getUserPage")
    public Result getUserPage(Long userId) {
       UserPageVo list = userService.getUserPage(userId);
        return Result.success(list);
    }
    @GetMapping("/getUserSpace")
    public Result getUserSpace(Long userId)
    {
        UserPageVo userSpaceVo = userService.getUserSpace(userId);
    return Result.success(userSpaceVo);
    }

    @GetMapping("/getUserInfoByUserId")
    public Result getUserInfo(Long userId)
    {
        Userinfo userinfo = userInfoMapper.selectOne(new LambdaQueryWrapper<Userinfo>().eq(Userinfo::getUserId, userId));
        return Result.success(userinfo);
    }

}
